cd "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\"
/*************************************/
/* Inflows into the occupation group */
/*************************************/
capture program drop inflows
program define inflows
version 11
syntax, occp(name)

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\unbalanced.dta", clear
drop cad2
gen cad2=(cs2h=="33" | cs2h=="34" | cs2h=="37" | cs2h=="38")

gen orig=(ptreat==1 & `occp'==1 )
/* gen inflow=(dtreat==1 & bc==1 & (L.bc!=1 | L.ize!=ize) & !missing(L.ize)) */
gen inflowr=(dtreat==1 & `occp'==1 & L.ize!=ize )

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 

xi , pre(REG_) i.nreg

gen sond=12
replace sond=24 if an==2007

sum inflowr [aweight = sond]


save temp1.dta, replace
collapse (sum) norig=orig ninflow=inflowr [fweight = sond], by(ze an)
save p1.dta, replace
use temp1.dta, clear

/*****************************************************/
collapse dimm dimm75 Y_* REG_*, by(ze an)
joinby ze an using p1.dta, unm(m)
drop _merge

egen ize=group(ze)
egen time=group(an)
xtset ize time
gen inflr=(D.ninflow)/norig

drop if missing(dimm75)
/* OLS */
ivreg2 inflr dimm Y_*  REG_*, partial(Y_* REG_*) cl(ze)
estimates store `occp'ols

/* IV */
ivreg2 inflr (dimm  = dimm75 ) REG_* Y_* , partial(Y_* REG_*) cl(ze)
estimates store `occp'iv

keep ze an inflr
save tempX.dta, replace

use temp1.dta, clear
joinby ze an using tempX.dta, unm(m)
drop _merge

gen inflow=(`occp'==1 & L.ize==ize )
/*keep if dtreat==1*/
keep if `occp'==1
/************************************************************/
/* location in the decile of the initial group distribution */
/* fixed effects age x an */
xi i.age*i.an , pre(F_)
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd), by(ze an) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen inter75= dimm75 * L.qinit

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

ivreg2 inflr (dimm inter = dimm75 inter75) lqinit  Y_*  REG_* [aweight = iw2], partial(Y_* REG_*) cl(ze)
estimates store `occp'iv2

end

inflows, occp(all) 
inflows, occp(cad2) 
inflows, occp(tecemp2) 
inflows, occp(bc) 
inflows, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Tab21\Tab6_inflows_unb.rtf"
/* OLS */
esttab allols  cad2ols tecemp2ols bcols bcnqols ///
using "C:\Users\Public\Documents\Tab21\Tab6_inflows_unb.rtf" /// 
, append title("OLS_CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/* IV */
esttab alliv  cad2iv tecemp2iv bciv bcnqiv ///
using "C:\Users\Public\Documents\Tab21\Tab6_inflows_unb.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

 esttab alliv2  cad2iv2 tecemp2iv2 bciv2 bcnqiv2 ///
using "C:\Users\Public\Documents\Tab21\Tab6_inflows_unb.rtf" /// 
, append title("2SLS-CS")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
